/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Servlet for managing Administrator Information
*  Name: AdminManagerServlet.java
*  Author: www.pecats.com
*  Date: September 15, 2008
************************************************************/

package com.pecats.server.core;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import com.pecats.server.core.CustomerAccountManager;
import com.pecats.server.core.Customer;
import com.pecats.server.data.ConfigData;
import com.pecats.server.util.ParseXMLFile;

/**
 * Servlet used to manage various admin request like login, create, delete for the server module of OpenXplore
 */

public class AdminManagerServlet extends HttpServlet 
{

	RequestDispatcher dispatcher;
	public static String CONFIGDIR = "server-properties";
	public static String PATHSEPERATOR = "\\";
	public static String CONFIGFILE = "config.xml";

    /*
     * Method used to initialize AdminManagerServlet
     * @param config ServletConfig object
     * @exception ServletException
    */
    public void init(ServletConfig config) throws ServletException 
    {
		super.init(config);
		ParseXMLFile parseFile = new ParseXMLFile();
		parseFile.parseXML(CONFIGDIR + PATHSEPERATOR + CONFIGFILE);
    }

    /*
     * Method used to handle incoming http request.
     * @param request HttpServletRequest object from the incoming request
     * @param response HttpServletResponse object which forms the response
     * @exception ServletException
     * @exception IOException
    */
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException, IOException
	{		
    	
		boolean create = true;         
		HttpSession session = request.getSession(create);

		String action = (String) request.getParameter("action");
	
		if (action.equalsIgnoreCase("login"))
		{
			session.setAttribute("AdminEmail", request.getParameter("Email"));
			try
			{
				CustomerAccountManager customManager = new CustomerAccountManager();
				if ( ((String) request.getParameter("password")).equals(customManager.checkCustomerLogin(request.getParameter("Email"))))
				{
					request.setAttribute("loginmessage", "Login Successfull");
					request.setAttribute("AdminEmail", request.getParameter("Email"));
					dispatcher = getServletContext().getRequestDispatcher("/Admin-Console.jsp");		
					dispatcher.forward(request, response);
				}
				else
				{
					request.setAttribute("loginmessage", "Either your username or password is incorrect, please try again" );
					dispatcher = getServletContext().getRequestDispatcher("/Admin-Login.jsp");		
					dispatcher.forward(request, response);
				}
			}			
			catch (Exception ex) 
			{
            		throw new ServletException(ex);
	        	}
		}
		else		
		if ( ((String) session.getAttribute("AdminEmail")).equalsIgnoreCase((String) request.getParameter("AdminEmail")) );
		{
			if (action.equalsIgnoreCase("create"))
				{
					request.setAttribute("AdminEmail", request.getAttribute("AdminEmail"));
					dispatcher = getServletContext().getRequestDispatcher("/Create-Customer.jsp");		
					dispatcher.forward(request, response);
				}
			else
				if (action.equalsIgnoreCase("update"))
				{
					request.setAttribute("AdminEmail", request.getParameter("AdminEmail"));
				    	dispatcher = getServletContext().getRequestDispatcher("/Update-Customer.jsp");		
					dispatcher.forward(request, response);
				}
			else			
				if (action.equalsIgnoreCase("delete"))
				{       
					request.setAttribute("AdminEmail", request.getParameter("AdminEmail"));
					dispatcher = getServletContext().getRequestDispatcher("/Delete-Customer.jsp");		
					dispatcher.forward(request, response);	
				}
			else			
				if (action.equalsIgnoreCase("retrieve"))
				{
					request.setAttribute("AdminEmail", request.getParameter("AdminEmail"));
					dispatcher = getServletContext().getRequestDispatcher("/Retrieve-Customer.jsp");		
					dispatcher.forward(request, response);
				}
			else			
				if (action.equalsIgnoreCase("change"))
				{
					request.setAttribute("AdminEmail", request.getParameter("AdminEmail"));				
					dispatcher = getServletContext().getRequestDispatcher("/Change-Password.jsp");		
					dispatcher.forward(request, response);
				}
			if (action.equalsIgnoreCase("createmain"))
				{
				
					Customer customer = new Customer();		
					customer.setFirstname(request.getParameter("Firstname"));	
					customer.setLastname(request.getParameter("Lastname"));	
					customer.setAddress(request.getParameter("Address"));	
					customer.setCity(request.getParameter("City"));	
					customer.setCountry(request.getParameter("Country"));	
					customer.setPhone(Integer.parseInt(request.getParameter("Phone")));	
					customer.setCompany(request.getParameter("Company"));	
					customer.setEmail(request.getParameter("Email"));	
					customer.setPassword(request.getParameter("Password"));	

					CustomerAccountManager customManager = new CustomerAccountManager();
					Long customerid = customManager.createCustomer(customer);

					request.setAttribute("loginmessage", "Would you like to create one more Customer?" );
					request.setAttribute("AdminEmail", request.getParameter("AdminEmail"));
					dispatcher = getServletContext().getRequestDispatcher("/Create-Customer.jsp");		
					dispatcher.forward(request, response);
				}
			else
				if (action.equalsIgnoreCase("updatemain"))
				{
				
					Customer customer = new Customer();		
					customer.setFirstname(request.getParameter("Firstname"));	
					customer.setLastname(request.getParameter("Lastname"));	
					customer.setAddress(request.getParameter("Address"));	
					customer.setCity(request.getParameter("City"));	
					customer.setCountry(request.getParameter("Country"));	
					customer.setPhone(Integer.parseInt(request.getParameter("Phone")));	
					customer.setCompany(request.getParameter("Company"));	
					customer.setEmail(request.getParameter("Email"));	
					customer.setPassword(request.getParameter("Password"));	

					CustomerAccountManager customManager = new CustomerAccountManager();
					customManager.updateCustomer(customer);

					request.setAttribute("loginmessage", "Would you like to update one more Customer?" );
					request.setAttribute("AdminEmail", request.getParameter("AdminEmail"));
			    		dispatcher = getServletContext().getRequestDispatcher("/Update-Customer.jsp");		
					dispatcher.forward(request, response);
				}
			else			
				if (action.equalsIgnoreCase("deletemain"))
				{       
					CustomerAccountManager customManager = new CustomerAccountManager();
					customManager.deleteCustomer(request.getParameter("Email"));
					request.setAttribute("loginmessage", "Would you like to delete one more customer?" );
					request.setAttribute("AdminEmail", request.getParameter("AdminEmail"));
					dispatcher = getServletContext().getRequestDispatcher("/Delete-Customer.jsp");		
					dispatcher.forward(request, response);	
				}
			else			
				if (action.equalsIgnoreCase("retrievemain"))
				{
					CustomerAccountManager customManager = new CustomerAccountManager();
					Customer aCustomer = customManager.getCustomer(request.getParameter("Email"));
					request.setAttribute("Firstname", aCustomer.getFirstname());	
					request.setAttribute("Lastname", aCustomer.getLastname());	
					request.setAttribute("Address", aCustomer.getAddress());	
					request.setAttribute("City", aCustomer.getCity());	
					request.setAttribute("Country", aCustomer.getCountry());	
					request.setAttribute("Phone", aCustomer.getPhone());	
					request.setAttribute("Company", aCustomer.getCompany());	
					request.setAttribute("Email", aCustomer.getEmail());	
					request.setAttribute("Password", aCustomer.getPassword());
					request.setAttribute("AdminEmail", request.getParameter("AdminEmail"));
					dispatcher = getServletContext().getRequestDispatcher("/Retrieve-Results.jsp");		
					dispatcher.forward(request, response);
				}
			else			
				if (action.equalsIgnoreCase("changemain"))
				{
					CustomerAccountManager customManager = new CustomerAccountManager();
					if   (customManager.checkCustomerLogin( (String)request.getParameter("Email")).equalsIgnoreCase( (String)request.getParameter("OldPassword")))
					{
						customManager.updatePassword(((String)request.getParameter("Email")), ((String)request.getParameter("NewPassword")));
						request.setAttribute("loginmessage", "Thank You, Password Sucessfully changed" );				
						request.setAttribute("AdminEmail", request.getParameter("AdminEmail"));				
						dispatcher = getServletContext().getRequestDispatcher("/Admin-Console.jsp");		
						dispatcher.forward(request, response);
					}
					else
					{
						request.setAttribute("loginmessage", "Incorrect Old Password, Please try again");				
						request.setAttribute("AdminEmail", request.getParameter("AdminEmail"));				
						dispatcher = getServletContext().getRequestDispatcher("/Change-Password.jsp");		
						dispatcher.forward(request, response);
					}
				}
			else			
				if (action.equalsIgnoreCase("logout"))
				{
					session.removeAttribute("AdminEmail");
					if (session != null) 
					{
						session.invalidate();
					}

					dispatcher = getServletContext().getRequestDispatcher("/Admin-Login.jsp");		
					dispatcher.forward(request, response);
				}
		}
	} 
}